﻿using MediatR;
using S.EarTag.Domain.ApiResult;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;

namespace S.EarTag.API.Mediator.MediatorCommand.MarkCommand
{
    public class MarkListPageCommandHandler : IRequestHandler<MarkListPageCommand, ApiResult<List<MarkModel>>>
    {
        private readonly IMarkModelRepository _markModelRepository;
        public MarkListPageCommandHandler(IMarkModelRepository markModelRepository)
        {
            _markModelRepository = markModelRepository;
        }
        /// <summary>
        /// 多条件查询戴标数据+分页查询显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<MarkModel>>> Handle(MarkListPageCommand request, CancellationToken cancellationToken)
        {
            var list = await _markModelRepository.GetMarkListPage(request.EarTag, request.NewEITag, request.EITag, request.WearId, request.MarkState);
            ApiResult<List<MarkModel>> apiResult = new ApiResult<List<MarkModel>>();
            apiResult.Data= list;
            //总条数、总页数
            apiResult.totalCount = list.Count();
            apiResult.pageCount = (int)Math.Ceiling((apiResult.totalCount * 1.0) / request.pageSize);
            //分页
            apiResult.Data = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return apiResult;
        }
    }
}
